/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.controller.servlet;

import com.context.AppContext;
import com.controller.AbstractController;
import com.controller.ConceptoPagoPersonalController;
import com.controller.PeriodoController;
import com.controller.UbigeoController;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Rdelacruz
 */
public class DispatcherJsonServlet extends HttpServlet {
    
    
    public DispatcherJsonServlet() throws Exception {
       
    }
    

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //charset=UTF-8: para que se codifiquen bien los carecteres especiales: eñes, acentos y otros
        response.setContentType("application/json;charset=UTF-8");
        
        try {
            String c = request.getParameter("controller");
            String a = request.getParameter("action");
            AbstractController controller = AppContext.getInstancia().getControllerMapping().getControllers().get(c);
            if(controller instanceof UbigeoController) {
                UbigeoController ct = (UbigeoController) controller;
                if(a.equals("provincias")) {
                    ct.provincias(request, response);
                }  
                else if(a.equals("distritos")) {
                    ct.distritos(request, response);
                }
            }
            else if(controller instanceof PeriodoController) {
                PeriodoController ct = (PeriodoController) controller;
                if(a.equals("periodo")) {
                    ct.periodo(request, response);
                }
            }
            else if(controller instanceof ConceptoPagoPersonalController) {
                ConceptoPagoPersonalController ct = (ConceptoPagoPersonalController) controller;
                if(a.equals("json")) {
                    ct.json(request, response);
                }
            }
        } catch (Exception ex) {
            //Logger.getLogger(DispatcherJsonServlet.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
     
        
    }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
